// pages/patientPages/history/orderDetail/orderDetail.js
const ajax = require("../../../../utils/ajax.js")
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderDetail: {},
    doctorStatus: false,
    status: 1,
    openid: ""
  },
  wxLogin: function() {
    //授权openid
    wx.login({
      success: function(res) {
        if (res.code) {
          ajax.request({
            url: app.url.miniGetCode,
            data: {
              code: res.code
            }
          }).then(data => {
            if (data.code !== 200) {
              return wx.showToast({
                title: data.msg,
                icon: "none"
              })
            }
            wx.setStorageSync("openid", data.data.openid)
            that.judgePay(data.data.openid)
          })
        }
      }
    })
  },
  judgePay: function(openid) {
    let that = this
    //微信支付
    ajax.request({
      url: app.url.appointmentOrderPay,
      data: {
        pay_no: that.data.orderDetail.order.pay_no,
        openid: openid
      }
    }).then(res => {
      if (res.code !== 200) {
        return wx.showToast({
          title: res.msg,
          icon: "none"
        })
      }
      wx.requestPayment({
        'timeStamp': res.data.timeStamp,
        'nonceStr': res.data.nonceStr,
        'package': res.data.package,
        'signType': res.data.signType,
        'paySign': res.data.paySign,
        'success': function(res) {
          wx.redirectTo({
            url: '/pages/userPages/history/history',
          })
        },
        'fail': function(res) {
          wx.showToast({
            title: '支付失败',
          })
        }
      })
    })
  },
  payClick: function() {
    if (!this.data.orderDetail.order.can_pay) {
      return wx.showToast({
        title: '已过支付时间',
        icon: "none"
      })
    }
    let openid = wx.getStorageSync("openid")
    let that = this
    if (openid) {
      that.judgePay(openid)
    } else {
      that.wxLogin()
    }
  },
  noGoClick: function(e) {
    wx.showModal({
      title: '提示',
      content: '确定要设置取消吗',
      success(res) {
        if (res.confirm) {
          ajax.request({
            url: app.url.appointmentMiss,
            data: {
              id: e.currentTarget.dataset.id
            }
          }).then(res => {
            if (res.code !== 200) {
              return wx.showToast({
                title: res.msg,
                icon: "none"
              })
            }
            wx.showToast({
              title: "设置成功",
              icon: "none",
              success: function() {
                wx.navigateBack({
                  delta: 1
                })
              }
            })
          })
        }
      }
    })

  },
  lookImg: function(e) {
    let url = e.currentTarget.dataset.url
    let that = this
    wx.previewImage({
      current: url, // 当前显示图片的http链接
      urls: that.data.orderDetail.cases // 需要预览的图片http链接列表
    })
  },
  sureGoClick: function(e) {
    wx.showModal({
      title: '提示',
      content: '确定要设置入座吗',
      success(res) {
        if (res.confirm) {
          ajax.request({
            url: app.url.appointmentSucces,
            data: {
              id: e.currentTarget.dataset.id
            }
          }).then(res => {
            if (res.code !== 200) {
              return wx.showToast({
                title: res.msg,
                icon: "none"
              })
            }
            wx.showToast({
              title: "设置成功",
              icon: "none",
              success: function() {
                wx.navigateBack({
                  delta: 1
                })
              }
            })
          })
        }
      }
    })
  },
  cancelClick: function(e) {
    let id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '确定要取消吗',
      confirmColor: "#4AB5F6",
      success(res) {
        if (res.confirm) {
          ajax.request({
            url: app.url.appointmentCancle,
            data: {
              id: id
            }
          }).then(res => {
            if (res.code == 200) {
              wx.showToast({
                title: '取消成功',
                icon: "none",
                duration: 3000,
                success: function() {
                  setTimeout(() => {
                    wx.navigateBack({
                      delta: 1
                    })
                  }, 2000)
                }
              })
            } else {
              wx.showToast({
                title: res.msg,
                icon: "none"
              })
            }
          })
        } else if (res.cancel) {

        }
      }
    })

  },
  deleteOrder (e){
    let id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '确定要删除吗',
      confirmColor: "#4AB5F6",
      success(res) {
        if (res.confirm) {
          ajax.request({
            url: app.url.appointmentDelete,
            data: {
              id: id
            }
          }).then(res => {
            if (res.code == 200) {
              wx.showToast({
                title: '删除成功',
                icon: "none",
                duration: 3000,
                success: function () {
                  setTimeout(() => {
                    wx.navigateBack({
                      delta: 1
                    })
                  }, 2000)
                }
              })
            } else {
              wx.showToast({
                title: res.msg,
                icon: "none"
              })
            }
          })
        } else if (res.cancel) {

        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    if (options.type == 'doctor') {
      this.setData({
        doctorStatus: true
      })
    }
    this.setData({
      status: options.status
    })

    ajax.request({
      url: app.url.appointmentDetail,
      data: {
        id: options.id
      }
    }).then(res => {
      if(res.code != 200){
        return wx.showToast({
          title: res.msg,
          icon:"none"
        })
      }
      this.setData({
        orderDetail: res.data
      })
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})